package it.unical.mat.aec.gui;

import it.unical.mat.aec.manageResult.ManageResultFacade;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class InputOrdinaFrame extends JFrame {
	
	final private JPanel contenentPanel = new JPanel(new GridBagLayout());
	private JRadioButton buttonOrdinaStudente;
	private JRadioButton buttonOrdinaVoto;
	private JRadioButton buttonOrdinaCrescente;
	private JRadioButton buttonOrdinaDecrescente;
	private final ModificaPanel modificaPanel;

	
	public InputOrdinaFrame(final ModificaPanel modificaPanel) {
		this.modificaPanel = modificaPanel;
		this.setSize(400, 250);
		this.setTitle("Selezione l'ordinamento");
		this.setLocation(200,200);
		this.setVisible(true);
		ButtonGroup groupOrdina=new ButtonGroup();
		buttonOrdinaStudente = new JRadioButton("Ordina per studente");
		buttonOrdinaVoto = new JRadioButton("Ordina per voto");
		buttonOrdinaVoto.setSelected(true);
		groupOrdina.add(buttonOrdinaVoto);
		groupOrdina.add(buttonOrdinaStudente);
		ButtonGroup groupTipo=new ButtonGroup();
		buttonOrdinaCrescente = new JRadioButton("Ordina crescente");
		buttonOrdinaCrescente.setSelected(true);
		buttonOrdinaDecrescente = new JRadioButton("Ordina per decrescente");
		groupTipo.add(buttonOrdinaDecrescente);
		groupTipo.add(buttonOrdinaCrescente);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets=new Insets(15, 15, 15, 15);
		constraints.anchor=GridBagConstraints.WEST;
		constraints.gridx=0;
		constraints.gridy=0;
		contenentPanel.add(buttonOrdinaVoto,constraints);
		constraints.gridy++;
		contenentPanel.add(buttonOrdinaStudente,constraints);
		constraints.gridy=0;
		constraints.gridx++;
		contenentPanel.add(buttonOrdinaCrescente,constraints);
		constraints.gridy++;
		contenentPanel.add(buttonOrdinaDecrescente,constraints);
		constraints.gridy++;
		JButton button=new JButton("Ordina");
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				InputOrdinaFrame.this.setVisible(false);
				String tipo="";
				if(isSelectedOrdinaPerStudente())
					tipo=ManageResultFacade.ORDINAMENTO_PER_STUDENTE;
				else if(isSelectedOrdinaPerVoto())
					tipo=ManageResultFacade.ORDINAMENTO_PER_VOTO;
				boolean crescente=false;
				if(isSelectedCrescente())
					crescente=true;
				modificaPanel.ordina(tipo, crescente);
			}
		});
		contenentPanel.add(button,constraints);
		add(contenentPanel);
	}
	
	public boolean isSelectedOrdinaPerVoto(){
		return buttonOrdinaVoto.isSelected();
	}
	
	public boolean isSelectedOrdinaPerStudente(){
		return buttonOrdinaStudente.isSelected();
	}
	
	public boolean isSelectedCrescente(){
		return buttonOrdinaCrescente.isSelected();
	}
}
